home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP15 / PRINT3.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  2.6 KB  |  98 lines

  1. /*---------------------------------------
  2.    PRINT3.C -- Printing with Dialog Box
  3.                (c) Charles Petzold, 1996
  4.   ---------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. HDC  GetPrinterDC (void) ;              // in PRINT.C
  9. void PageGDICalls (HDC, int, int) ;
  10.  
  11. HINSTANCE hInst ;
  12. char      szAppName[] = "Print3" ;
  13. char      szCaption[] = "Print Program 3 (Dialog Box)" ;
  14.  
  15. BOOL   bUserAbort ;
  16. HWND   hDlgPrint ;
  17.  
  18. BOOL CALLBACK PrintDlgProc (HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  19.      {
  20.      switch (msg)
  21.           {
  22.           case WM_INITDIALOG :
  23.                SetWindowText (hDlg, szAppName) ;
  24.                EnableMenuItem (GetSystemMenu (hDlg, FALSE), SC_CLOSE,
  25.                                                             MF_GRAYED) ;
  26.                return TRUE ;
  27.  
  28.           case WM_COMMAND :
  29.                bUserAbort = TRUE ;
  30.                EnableWindow (GetParent (hDlg), TRUE) ;
  31.                DestroyWindow (hDlg) ;
  32.                hDlgPrint = 0 ;
  33.                return TRUE ;
  34.           }
  35.      return FALSE ;
  36.      }
  37.  
  38. BOOL CALLBACK AbortProc (HDC hdcPrn, int iCode)
  39.      {
  40.      MSG   msg ;
  41.  
  42.      while (!bUserAbort && PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  43.           {
  44.           if (!hDlgPrint || !IsDialogMessage (hDlgPrint, &msg))
  45.                {
  46.                TranslateMessage (&msg) ;
  47.                DispatchMessage (&msg) ;
  48.                }
  49.           }
  50.      return !bUserAbort ;
  51.      }
  52.  
  53. BOOL PrintMyPage (HWND hwnd)
  54.      {
  55.      static DOCINFO di     = { sizeof (DOCINFO), "Print3: Printing", NULL } ;
  56.      BOOL           bError = FALSE ;
  57.      HDC            hdcPrn ;
  58.      int            xPage, yPage ;
  59.  
  60.      if (NULL == (hdcPrn = GetPrinterDC ()))
  61.           return TRUE ;
  62.  
  63.      xPage = GetDeviceCaps (hdcPrn, HORZRES) ;
  64.      yPage = GetDeviceCaps (hdcPrn, VERTRES) ;
  65.  
  66.      EnableWindow (hwnd, FALSE) ;
  67.  
  68.      bUserAbort = FALSE ;
  69.      hDlgPrint = CreateDialog (hInst, "PrintDlgBox", hwnd, PrintDlgProc) ;
  70.  
  71.      SetAbortProc (hdcPrn, AbortProc) ;
  72.  
  73.      if (StartDoc (hdcPrn, &di) > 0)
  74.           {
  75.           if (StartPage (hdcPrn) > 0)
  76.                {
  77.                PageGDICalls (hdcPrn, xPage, yPage) ;
  78.  
  79.                if (EndPage (hdcPrn) > 0)
  80.                     EndDoc (hdcPrn) ;
  81.                else
  82.                     bError = TRUE ;
  83.                }
  84.           }
  85.      else
  86.           bError = TRUE ;
  87.  
  88.      if (!bUserAbort)
  89.           {
  90.           EnableWindow (hwnd, TRUE) ;
  91.           DestroyWindow (hDlgPrint) ;
  92.           }
  93.  
  94.      DeleteDC (hdcPrn) ;
  95.  
  96.      return bError || bUserAbort ;
  97.      }
  98.